import Vue from 'vue'
import { v4 as uuidv4 } from "uuid";
import modal from '../../plugins/modal'
const vm = new Vue();
const model_scenario = {
    state: {
        componentList: [
            {
                id: 1,
                name: "模型管理工具组件",
                path: "/model-manage-tool",
                component: "ModelManageTool",
            },
            {
                id: 2,
                name: "想定编辑工具组件",
                path: "/scenario-edit-tool",
                component: "ScenarioEditTool",
            },
        ],
        categoryTreeData: [
            {
                "key": "apiSchemaFolder.12008725",
                "type": "apiSchemaFolder",
                "name": "武器",
                "children": [
                    {
                        "key": "apiSchemaFolder.12051093",
                        "type": "apiSchemaFolder",
                        "name": "xhd",
                        "children": [{
                            "key": "apiSchema.127599553",
                            "type": "apiSchema",
                            "name": "通用巡航弹",
                            "children": [],
                            "schema": {
                                "id": 127599553,
                                "name": "通用巡航弹",
                                "displayName": ""
                            },
                            "displayName": ""
                        }
                        ],
                        "folder": {
                            "id": 12051093,
                            "name": "xhd",
                            "parentId": 12008725
                        }
                    },
                    {
                        "key": "apiSchemaFolder.12051094",
                        "type": "apiSchemaFolder",
                        "name": "ddd",
                        "children": [{
                            "key": "apiSchema.127599550",
                            "type": "apiSchema",
                            "name": "普通ddd",
                            "children": [],
                            "schema": {
                                "id": 127599550,
                                "name": "普通ddd",
                                "displayName": ""
                            },
                            "displayName": ""
                        }
                        ],
                        "folder": {
                            "id": 12051094,
                            "name": "ddd",
                            "parentId": 12008725
                        }
                    },
                ],
                "folder": {
                    "id": 12008725,
                    "name": "武器",
                    "parentId": 0
                }
            },
            {
                "key": "apiSchemaFolder.12008726",
                "type": "apiSchemaFolder",
                "name": "干扰",
                "children": [],
                "folder": {
                    "id": 12008725,
                    "name": "干扰",
                    "parentId": 0
                }
            },
        ],
        total_menu_list: {
            // 筛选按钮右侧+的展开菜单
            total_create_list: [
                {
                    id: 1,
                    name: "新增分类",
                    icon: "create-directory",
                    func: "handleCreateDirectory",
                },
                {
                    id: 2,
                    name: "新增模型",
                    icon: "model",
                    func: "handleCreateModel",
                },
                {
                    id: 3,
                    name: "导入数据",
                    icon: "file-import",
                    func: "handleImportFile",
                },
            ],
            // 类别列表右侧+的展开菜单
            category_create_list: [
                {
                    id: 1,
                    name: "新增数据模型",
                    icon: "model",
                    func: "handleCreateModel",
                },
                {
                    id: 2,
                    name: "新增分类",
                    icon: "create-directory",
                    func: "handleCreateDirectory",
                },
                {
                    id: 3,
                    name: "导入文件",
                    icon: "file-import",
                    func: "handleImportFile",
                },
            ],
            // 类别列表右侧...的展开菜单
            category_more_list: [
                {
                    id: 1,
                    name: "导出文件",
                    icon: "file-export",
                    func: "handleExportFile",
                },
            ],
            // 每个类别文件夹右侧...的展开菜单
            category_folder_list: [
                {
                    id: 1,
                    name: "新增数据模型",
                    icon: "model",
                    func: "handleCreateModel",
                },
                {
                    id: 2,
                    name: "导入文件",
                    icon: "file-import",
                    func: "handleImportFile",
                },
                {
                    id: 3,
                    name: "编辑",
                    icon: "rename",
                    func: "handleCreateDirectory",
                },
                {
                    id: 4,
                    name: "复制",
                    icon: "copy",
                    func: "handleCopy",
                },
                {
                    id: 5,
                    name: "添加子目录",
                    icon: "create-directory",
                    func: "handleCreateDirectory",
                },
                {
                    id: 6,
                    name: "导出文件",
                    icon: "file-export",
                    func: "handleImportFile",
                },
                {
                    id: 7,
                    name: "删除",
                    icon: "delete",
                    func: "handleDelete",
                },
            ],
            // 每个类别模型右侧...的展开菜单
            category_model_list: [
                {
                    id: 1,
                    name: "复制",
                    icon: "copy",
                    func: "handleCopy",
                },
                {
                    id: 2,
                    name: "移动到",
                    icon: "move-file",
                    func: "handleMoveFile",
                },
                {
                    id: 3,
                    name: "导出文件",
                    icon: "file-export",
                    func: "handleExportFile",
                },
                {
                    id: 4,
                    name: "删除",
                    icon: "delete",
                    func: "handleDelete",
                },
            ]
        },
        menu_list: {
            // 节点操作中新增按钮的菜单
            list1: [
                {
                    id: 1,
                    name: "添加相邻节点",
                },
                {
                    id: 2,
                    name: "添加子节点",
                }
            ],
            // 节点操作中更多按钮的菜单
            list2: [{
                id: 1,
                name: "提取为数据模型",
                icon: "data-model-extraction",
                i18nName: "extractDataModel"
            }],
            // 节点类型popover菜单数据
            list3: [
                {
                    id: 1,
                    name: "引用模型",
                    value: "quote",
                },
                {
                    id: 2,
                    name: "string",
                    value: "string",
                },
                {
                    id: 3,
                    name: "integer",
                    value: "integer",
                },
                {
                    id: 4,
                    name: "boolean",
                    value: "boolean",
                },
                {
                    id: 5,
                    name: "array",
                    value: "array",
                },
                {
                    id: 6,
                    name: "object",
                    value: "object",
                },
                {
                    id: 7,
                    name: "number",
                    value: "number",
                },
                {
                    id: 8,
                    name: "dictionary",
                    value: "dictionary",
                },
                // {
                //     id: 9,
                //     name: "null",
                //     value: "null",
                // },
                // {
                //     id: 10,
                //     name: "any",
                //     value: "any",
                // },

            ],
            // 高级设置的数据类型下拉菜单
            list4: [
                {
                    label: "引用",
                    options: [
                        {
                            label: "引用模型",
                            value: "quote",
                        },
                    ],
                },
                {
                    label: "基础类型",
                    options: [
                        {
                            label: "string",
                            value: "string",
                        },
                        {
                            label: "integer",
                            value: "integer",
                        },
                        {
                            label: "boolean",
                            value: "boolean",
                        },
                        {
                            label: "array",
                            value: "array",
                        },
                        {
                            label: "object",
                            value: "object",
                        },
                        {
                            label: "number",
                            value: "number",
                        },
                        // {
                        //     label: "null",
                        //     value: "null",
                        // },
                        // {
                        //     label: "any",
                        //     value: "any",
                        // },
                    ],
                },
                {
                    label: "高级",
                    options: [
                        {
                            label: "dictionary",
                            value: "dictionary",
                        }
                    ],
                },
            ],
            // 高级设置的behavior下拉菜单
            list5: [
                {
                    label: "Read/Write",
                    value: "RW",
                },
                {
                    label: "Read Only",
                    value: "R",
                },
                {
                    label: "Write Only",
                    value: "W",
                },
            ],
            list6: [
                {
                    id: 1,
                    name: "添加相邻节点",
                }
            ],
            list7: [
                {
                    id: 2,
                    name: "添加子节点",
                }
            ]
        },
        data_model_tree_data: [
            {
                "key": "apiSchemaFolder.11008725",
                "type": "apiSchemaFolder",
                "name": "巡航弹",
                "children": [
                    {
                        "key": "apiSchemaFolder.11051093",
                        "type": "apiSchemaFolder",
                        "name": "指令",
                        "children": [
                            {
                                "key": "apiSchema.127599553",
                                "type": "apiSchema",
                                "name": "offensive_mis_launch",
                                "children": [],
                                "schema": {
                                    "id": 127599553,
                                    "name": "offensive_mis_launch",
                                    "displayName": "进攻弹发射"
                                },
                                "displayName": "进攻弹发射"
                            },
                            {
                                "key": "apiSchema.127599572",
                                "type": "apiSchema",
                                "name": "offensive_mis_guid",
                                "children": [],
                                "schema": {
                                    "id": 127599572,
                                    "name": "offensive_mis_guid",
                                    "displayName": "进攻弹制导"
                                },
                                "displayName": "进攻弹制导"
                            }
                        ],
                        "folder": {
                            "id": 11051093,
                            "name": "指令",
                            "parentId": 11008725
                        }
                    },
                    {
                        "key": "apiSchemaFolder.11051094",
                        "type": "apiSchemaFolder",
                        "name": "事件",
                        "children": [
                            {
                                "key": "apiSchema.127599554",
                                "type": "apiSchema",
                                "name": "missile_launch",
                                "children": [],
                                "schema": {
                                    "id": 127599554,
                                    "name": "missile_launch",
                                    "displayName": "导弹发射事件"
                                },
                                "displayName": "导弹发射事件"
                            },
                        ],
                        "folder": {
                            "id": 11051094,
                            "name": "事件",
                            "parentId": 11008725
                        }
                    },
                    {
                        "key": "apiSchemaFolder.11051110",
                        "type": "apiSchemaFolder",
                        "name": "型号属性",
                        "children": [
                        ],
                        "folder": {
                            "id": 11051110,
                            "name": "型号属性",
                            "parentId": 11008725
                        }
                    },
                    {
                        "key": "apiSchemaFolder.11051111",
                        "type": "apiSchemaFolder",
                        "name": "额外属性",
                        "children": [
                        ],
                        "folder": {
                            "id": 11051111,
                            "name": "额外属性",
                            "parentId": 11008725
                        }
                    },
                ],
                "folder": {
                    "id": 11008725,
                    "name": "巡航弹",
                    "parentId": 0
                }
            }
        ],
        data_model_data: [
            {
                "id": 127599553,
                "name": "offensive_missile_launch",
                "displayName": "进攻弹发射",
                "jsonSchema": {
                    "id": uuidv4(),
                    "type": "object",
                    "properties": {
                        "name": {
                            "id": uuidv4(),
                            "type": "string",
                            "title": "名称",
                            "description": "名字"
                        },
                        "externalInfo": {
                            "id": uuidv4(),
                            "type": "object",
                            "properties": {
                                "id": {
                                    "id": uuidv4(),
                                    "type": "number",
                                    "title": "id",
                                    "description": "唯一标识"
                                },
                                "nameChn": {
                                    "id": uuidv4(),
                                    "type": "string",
                                    "title": "中文名称",
                                    "description": "中文名称"
                                },
                                "nameEn": {
                                    "id": uuidv4(),
                                    "type": "string",
                                    "title": "英文名称",
                                    "description": "英文名称"
                                },
                                "status": {
                                    "id": uuidv4(),
                                    "type": "boolean",
                                    "description": "状态码，0表示否，1表示是",
                                    "title": "状态"
                                },
                                "optionalModels": {
                                    "id": uuidv4(),
                                    "type": "array",
                                    "items": {
                                        "id": uuidv4(),
                                        "type": "number"
                                    },
                                    "title": "可选型号列表",
                                    "description": "数组格式的可选型号列表"
                                },
                                "sign": {
                                    "id": uuidv4(),
                                    "type": "null",
                                    "title": "标识",
                                    "description": "具体细节"
                                },
                                "weather": {
                                    "id": uuidv4(),
                                    "type": [
                                        "string",
                                        "integer",
                                        "boolean",
                                        "array",
                                        "object",
                                        "number",
                                        "null"
                                    ],
                                    "title": "天气",
                                    "description": "凑数属性"
                                }
                            },
                            "x-apifox-orders": [
                                "id",
                                "nameChn",
                                "nameEn",
                                "status",
                                "optionalModels",
                                "sign",
                                "weather"
                            ],
                            "required": [
                                "id",
                                "nameChn",
                                "nameEn",
                                "status",
                                "optionalModels",
                                "sign",
                                "weather"
                            ],
                            "title": "额外信息",
                            "description": "为后期做准备"
                        },
                        "description": {
                            "id": uuidv4(),
                            "type": [
                                "string",
                                "null"
                            ],
                            "title": "描述",
                            "description": "细节说明"
                        }
                    },
                    "x-apifox-orders": [
                        "name",
                        "description",
                        "01JBDHM6TKKX1HENMRK6NNZEPK",
                        "externalInfo"
                    ],
                    "required": [
                        "name",
                        "externalInfo",
                        "description"
                    ],
                    "x-apifox-refs": {
                        "01JBDHM6TKKX1HENMRK6NNZEPK": {
                            "$ref": "#/definitions/127599553"
                        }
                    },
                    "title": "测试",
                    "description": "测试数据结构"
                },
                "folderId": 11051093,
                "description": "",
                "projectId": 5190041,
                "ordering": 10,
                "creatorId": 2554704,
                "editorId": 2554704,
                "createdAt": "2024-10-29T08:17:15.000Z",
                "updatedAt": "2024-10-29T08:17:21.000Z",
                "visibility": "INHERITED"
            },
            {
                "id": 127599572,
                "name": "offensive_missile_guidance",
                "displayName": "进攻弹制导",
                "jsonSchema": {
                    "id": uuidv4(),
                    "type": "object",
                    "properties": {
                        "name": {
                            "id": uuidv4(),
                            "type": "string",
                            "title": "名称",
                            "description": "名字"
                        },
                        "externalInfo": {
                            "id": uuidv4(),
                            "type": "object",
                            "properties": {
                                "id": {
                                    "id": uuidv4(),
                                    "type": "number",
                                    "title": "id",
                                    "description": "唯一标识"
                                },
                                "nameChn": {
                                    "id": uuidv4(),
                                    "type": "string",
                                    "title": "中文名称",
                                    "description": "中文名称"
                                },
                                "nameEn": {
                                    "id": uuidv4(),
                                    "type": "string",
                                    "title": "英文名称",
                                    "description": "英文名称"
                                },
                                "status": {
                                    "id": uuidv4(),
                                    "type": "boolean",
                                    "description": "状态码，0表示否，1表示是",
                                    "title": "状态"
                                },
                                "optionalModels": {
                                    "id": uuidv4(),
                                    "type": "array",
                                    "items": {
                                        "type": "number"
                                    },
                                    "title": "可选型号列表",
                                    "description": "数组格式的可选型号列表"
                                },
                                "sign": {
                                    "id": uuidv4(),
                                    "type": "null",
                                    "title": "标识",
                                    "description": "具体细节"
                                },
                                "weather": {
                                    "id": uuidv4(),
                                    "type": [
                                        "string",
                                        "integer",
                                        "boolean",
                                        "array",
                                        "object",
                                        "number",
                                        "null"
                                    ],
                                    "title": "天气",
                                    "description": "凑数属性"
                                }
                            },
                            "x-apifox-orders": [
                                "id",
                                "nameChn",
                                "nameEn",
                                "status",
                                "optionalModels",
                                "sign",
                                "weather"
                            ],
                            "required": [
                                "id",
                                "nameChn",
                                "nameEn",
                                "status",
                                "optionalModels",
                                "sign",
                                "weather"
                            ],
                            "title": "额外信息",
                            "description": "为后期做准备"
                        },
                        "description": {
                            "id": uuidv4(),
                            "type": [
                                "string",
                                "null"
                            ],
                            "title": "描述",
                            "description": "细节说明"
                        }
                    },
                    "x-apifox-orders": [
                        "name",
                        "description",
                        "01JBDHM6TKKX1HENMRK6NNZEPK",
                        "externalInfo"
                    ],
                    "required": [
                        "name",
                        "externalInfo",
                        "description"
                    ],
                    "x-apifox-refs": {
                        "01JBDHM6TKKX1HENMRK6NNZEPK": {
                            "$ref": "#/definitions/127599553"
                        }
                    },
                    "title": "测试",
                    "description": "测试数据结构"
                },
                "folderId": 11051093,
                "description": "",
                "projectId": 5190041,
                "ordering": 20,
                "creatorId": 2554704,
                "editorId": 2554704,
                "createdAt": "2024-10-29T08:17:33.000Z",
                "updatedAt": "2024-10-29T08:17:33.000Z",
                "visibility": "INHERITED"
            },
            {
                "id": 127599554,
                "name": "missile_launch",
                "displayName": "导弹发射",
                "jsonSchema": {
                    "id": uuidv4(),
                    "type": "object",
                    "properties": {
                        "name": {
                            "type": "string",
                            "title": "",
                            "description": ""
                        }
                    },
                    "x-apifox-orders": [
                        "name"
                    ],
                    "required": [
                        "name"
                    ]
                },
                "folderId": 11051094,
                "description": "",
                "projectId": 5190041,
                "ordering": 20,
                "creatorId": 2554704,
                "editorId": 2554704,
                "createdAt": "2024-10-29T08:17:33.000Z",
                "updatedAt": "2024-10-29T08:17:33.000Z",
                "visibility": "INHERITED"
            },
        ],
        format_options: {
            integer_format_options: [
                {
                    label: "int8",
                    value: "int8",
                },
                {
                    label: "int16",
                    value: "int16",
                },
                {
                    label: "int32",
                    value: "int32",
                },
                {
                    label: "int64",
                    value: "int64",
                },
                {
                    label: "uint8",
                    value: "uint8",
                },
                {
                    label: "uint16",
                    value: "uint16",
                },
                {
                    label: "uint32",
                    value: "uint32",
                },
                {
                    label: "uint64",
                    value: "uint64",
                },
            ],
            string_format_options: [
                {
                    label: "date-time",
                    value: "date-time",
                },
                {
                    label: "date",
                    value: "date",
                },
                {
                    label: "time",
                    value: "time",
                },
                {
                    label: "duration",
                    value: "duration",
                },
                {
                    label: "email",
                    value: "email",
                },
                {
                    label: "idn-email",
                    value: "idn-email",
                },
                {
                    label: "hostname",
                    value: "hostname",
                },
                {
                    label: "idn-hostname",
                    value: "idn-hostname",
                },
                {
                    label: "ipv4",
                    value: "ipv4",
                },
                {
                    label: "ipv6",
                    value: "ipv6",
                },
                {
                    label: "uri",
                    value: "uri",
                },
                {
                    label: "uri-reference",
                    value: "uri-reference",
                },
                {
                    label: "iri",
                    value: "iri",
                },
                {
                    label: "iri-reference",
                    value: "iri-reference",
                },
                {
                    label: "uuid",
                    value: "uuid",
                },
                {
                    label: "uri-template",
                    value: "uri-template",
                },
                {
                    label: "json-pointer",
                    value: "json-pointer",
                },
                {
                    label: "relative-json-pointer",
                    value: "relative-json-pointer",
                },
                {
                    label: "regex",
                    value: "regex",
                },
                {
                    label: "binary",
                    value: "binary",
                },
                {
                    label: "byte",
                    value: "byte",
                },
                {
                    label: "password",
                    value: "password",
                },
                {
                    label: "char",
                    value: "char",
                },
            ],
            number_format_options: [
                {
                    label: "int8",
                    value: "int8",
                },
                {
                    label: "int16",
                    value: "int16",
                },
                {
                    label: "int32",
                    value: "int32",
                },
                {
                    label: "int64",
                    value: "int64",
                },
                {
                    label: "uint8",
                    value: "uint8",
                },
                {
                    label: "uint16",
                    value: "uint16",
                },
                {
                    label: "uint32",
                    value: "uint32",
                },
                {
                    label: "uint64",
                    value: "uint64",
                },
                {
                    label: "float",
                    value: "float",
                },
                {
                    label: "double",
                    value: "double",
                },
                {
                    label: "float32",
                    value: "float32",
                },
                {
                    label: "float64",
                    value: "float64",
                },
            ],
        }
    },
    mutations: {
        SET_DATA_MODEL_DATA: (state, data_model_data) => {
            state.data_model_data = data_model_data;
        }
    },

    actions: {
        // 新增节点
        addSchemaStructure({ commit }, params) {
            return new Promise((resolve, reject) => {
                let { totalSchema, currentSchema, node, insert } = params;
                totalSchema.forEach(item => {
                    if (item.id == currentSchema.schema.id) {
                        if (node.root) {
                            if (checkRepeat(item.jsonSchema.properties, "字段名称")) {
                                this.$modal.msgError('字段  字段名称  重复，请修改！');
                                return;
                            }
                            // 点击了根节点的新增按钮 新增子节点
                            vm.$set(item.jsonSchema, 'properties', {
                                ...item.jsonSchema.properties,
                                "字段名称": {
                                    id: uuidv4(),
                                    type: "string",
                                    title: '',
                                    description: ''
                                }
                            })
                            // 新增排序数据中的节点名称
                            if (item.jsonSchema['x-apifox-orders']) {
                                item.jsonSchema['x-apifox-orders'].push('字段名称')
                            }
                            // 新增必填数据中的节点名称
                            if (item.jsonSchema['required']) {
                                item.jsonSchema['required'].push('字段名称')
                            }
                        } else {
                            // 点击了非根节点的新增按钮 新增 相邻节点/子节点
                            recurNode(item.jsonSchema, node.id, insert);
                        }

                    }
                })
                function recurNode(jsonSchema, id, addType) {
                    Object.keys(jsonSchema.properties).forEach((item) => {
                        if (jsonSchema.properties[item].id == id) {
                            // 点击的当前节点，除非type为object，否则新增类型只可能为相邻节点，也可通过addType判断
                            if (addType) {
                                if (checkRepeat(jsonSchema.properties[item].properties, "字段名称")) {
                                    modal.msgError('字段  字段名称  重复，请修改！');
                                    return;
                                }
                                vm.$set(jsonSchema.properties[item], 'properties', {
                                    ...jsonSchema.properties[item].properties,
                                    "字段名称": {
                                        id: uuidv4(),
                                        type: "string",
                                        title: '',
                                        description: ''
                                    }
                                })
                                // 新增排序数据中的节点名称
                                if (jsonSchema.properties[item]['x-apifox-orders']) {
                                    jsonSchema.properties[item]['x-apifox-orders'].push('字段名称')
                                }
                                // 新增必填数据中的节点名称
                                if (jsonSchema.properties[item]['required']) {
                                    jsonSchema.properties[item]['required'].push('字段名称')
                                }
                            } else {
                                if (checkRepeat(jsonSchema.properties, "字段名称")) {
                                    modal.msgError('字段  字段名称  重复，请修改！');
                                    return;
                                }
                                // 在当前节点后添加新节点
                                let index = Object.keys(jsonSchema.properties).findIndex(element => jsonSchema.properties[element].id == id)
                                let order = Object.keys(jsonSchema.properties);
                                order.splice(index + 1, 0, "字段名称");
                                jsonSchema.properties['字段名称'] = {
                                    id: uuidv4(),
                                    type: "string",
                                    title: '',
                                    description: ''
                                }
                                jsonSchema.properties = reorderObject(jsonSchema.properties, order);
                                // 新增排序数据中的节点名称
                                if (jsonSchema['x-apifox-orders']) {
                                    jsonSchema['x-apifox-orders'].splice(index + 1, 0, "字段名称");
                                }
                                // 新增必填数据中的节点名称
                                if (jsonSchema['required']) {
                                    jsonSchema['required'].splice(index + 1, 0, "字段名称")
                                }
                            }
                        } else {
                            if (jsonSchema.properties[item].properties) {
                                recurNode(jsonSchema.properties[item], id, addType);
                            }
                        }
                    });
                }
                // 查重，不允许有相同的名称
                function checkRepeat(jsonSchema, attrName) {
                    let repeat = false;
                    Object.keys(jsonSchema).forEach(item => {
                        if (item == attrName) {
                            repeat = true
                        }
                    })
                    return repeat
                }
                // 对数组中的属性进行重新排序
                function reorderObject(originalObj, order) {
                    const newObj = {};

                    // 按定义的顺序添加属性到新对象
                    order.forEach(key => {
                        if (originalObj.hasOwnProperty(key)) {
                            newObj[key] = originalObj[key];
                        }
                    });

                    // 添加原对象中未在排序数组中的属性
                    for (const key in originalObj) {
                        if (originalObj.hasOwnProperty(key) && !newObj.hasOwnProperty(key)) {
                            newObj[key] = originalObj[key];
                        }
                    }

                    return newObj;
                }
                resolve(totalSchema)
                commit('SET_DATA_MODEL_DATA', totalSchema)
            })
        },
        // 更新当前节点的名称
        updateSchemaName({ commit }, params) {
            return new Promise((resolve, reject) => {
                let { totalSchema, currentSchema, node, value } = params;
                totalSchema.forEach(item => {
                    if (item.id == currentSchema.schema.id) {
                        if (item.jsonSchema.id == node.id) {
                        } else {
                            // 非根节点修改节点名称
                            recurNode(item.jsonSchema, node, value);
                        }

                    }
                })
                function recurNode(jsonSchema, node, value) {
                    const staticData = jsonSchema.properties;
                    Object.keys(jsonSchema.properties).forEach((item) => {
                        if (jsonSchema.properties[item].id == node.id) {
                            if (checkRepeat(jsonSchema.properties, value)) {
                                modal.msgError(`字段  ${value}  重复，请修改！`);
                                return;
                            }
                            let index = Object.keys(jsonSchema.properties).findIndex(element => jsonSchema.properties[element].id == node.id);
                            let order = Object.keys(jsonSchema.properties);
                            order[index] = node.value;
                            jsonSchema.properties[node.value] = staticData[item];
                            delete jsonSchema.properties[item];
                            jsonSchema.properties = reorderObject(jsonSchema.properties, order);
                            // 更新排序数据中的节点名称
                            if (jsonSchema['x-apifox-orders'] && jsonSchema['x-apifox-orders'].includes(item)) {
                                jsonSchema['x-apifox-orders'] = jsonSchema['x-apifox-orders'].map(element => element === item ? node.value : element)
                            }
                            // 更新必填数据中的节点名称
                            if (jsonSchema['required'] && jsonSchema['required'].includes(item)) {
                                jsonSchema['required'] = jsonSchema['required'].map(element => element === item ? node.value : element)
                            }
                        } else {
                            if (jsonSchema.properties[item]['properties']) {
                                recurNode(jsonSchema.properties[item], node, value);
                            }
                        }
                    });
                }
                // 查重，不允许有相同的名称
                function checkRepeat(jsonSchema, attrName) {
                    let repeat = false;
                    Object.keys(jsonSchema).forEach(item => {
                        if (item == attrName) {
                            repeat = true
                        }
                    })
                    return repeat
                }
                // 对数组中的属性进行重新排序
                function reorderObject(originalObj, order) {
                    const newObj = {};

                    // 按定义的顺序添加属性到新对象
                    order.forEach(key => {
                        if (originalObj.hasOwnProperty(key)) {
                            newObj[key] = originalObj[key];
                        }
                    });

                    // 添加原对象中未在排序数组中的属性
                    for (const key in originalObj) {
                        if (originalObj.hasOwnProperty(key) && !newObj.hasOwnProperty(key)) {
                            newObj[key] = originalObj[key];
                        }
                    }

                    return newObj;
                }
                resolve(totalSchema)
                commit('SET_DATA_MODEL_DATA', totalSchema)
            })
        },
        // 更新当前节点的format类型
        updateSchemaFormatType({ commit }, params) {
            return new Promise((resolve, reject) => {
                let { totalSchema, currentSchema, node, formatType } = params;
                totalSchema.forEach(item => {
                    if (item.id == currentSchema.schema.id) {
                        if (['string', 'integer', 'number'].includes(item.jsonSchema.type) && node.root) {
                            // 根节点是基础类型，且设置了format
                            vm.$set(item.jsonSchema, 'format', formatType)
                        } else {
                            // 非根节点设置format
                            recurNode(item.jsonSchema.properties, node.id, formatType);
                        }

                    }
                })
                function recurNode(jsonSchema, id, type) {
                    Object.keys(jsonSchema).forEach((item) => {
                        if (jsonSchema[item].id == id) {
                            vm.$set(jsonSchema[item], 'format', type);
                        } else {
                            if (jsonSchema[item].properties) {
                                recurNode(jsonSchema[item].properties, id, type);
                            }
                        }
                    });
                }
                resolve(totalSchema)
                commit('SET_DATA_MODEL_DATA', totalSchema)
            })

        },
        // 更新当前节点的数据类型
        updateSchemaType({ commit }, params) {
            return new Promise((resolve, reject) => {
                let { totalSchema, currentSchema, node, nodeType } = params;
                totalSchema.forEach(item => {
                    if (item.id == currentSchema.schema.id) {
                        if (node.root) {
                            // 根节点类型发生变化
                            vm.$set(item.jsonSchema, 'type', nodeType)
                        } else {
                            // 非根节点类型发生变化
                            recurNode(item.jsonSchema.properties, node.id, nodeType);
                        }

                    }
                })
                function recurNode(jsonSchema, id, type) {
                    Object.keys(jsonSchema).forEach(item => {
                        if (jsonSchema[item].id == id) {
                            vm.$set(jsonSchema[item], 'type', type == 'any' ? ["string", "integer", "boolean", "array", "object", "number", "null"] : type);
                            if (type === 'array') {
                                // 当前类型更换为array，则添加items属性
                                vm.$set(jsonSchema[item], 'items', { type: "string" })
                                if (jsonSchema[item].properties) {
                                    delete jsonSchema[item].properties;
                                }
                            } else if (type === 'object') {
                                // 当前类型更换为object，则添加properties属性
                                vm.$set(jsonSchema[item], 'properties', {
                                    "字段名称": {
                                        type: "string",
                                        title: '',
                                        description: ''
                                    }
                                })
                                if (jsonSchema[item].items) {
                                    delete jsonSchema[item].items;
                                }
                            } else {
                                // 其他类型，则删除items和properties属性
                                if (jsonSchema[item].items) {
                                    delete jsonSchema[item].items;
                                }
                                if (jsonSchema[item].properties) {
                                    delete jsonSchema[item].properties;
                                }
                            }
                        } else {
                            if (jsonSchema[item].properties) {
                                recurNode(jsonSchema[item].properties, id, type);
                            }
                        }
                    })
                }
                resolve(totalSchema)
                commit('SET_DATA_MODEL_DATA', totalSchema)
            })
        },
        // 更新当前节点的是否必填/允许NULL
        updateSchemaRequiredOrNull({ commit }, params) {
            return new Promise((resolve, reject) => {
                let { totalSchema, currentSchema, node, key } = params;
                totalSchema.forEach(item => {
                    if (item.id == currentSchema.schema.id) {
                        if (node.root) {
                            // 根节点只有允许NULL，无必填设置
                            vm.$set(item.jsonSchema, 'type', Array.isArray(item.jsonSchema.type) ? item.jsonSchema.type.filter(item => item != 'null').length > 1 ? item.jsonSchema.type.filter(item => item != 'null') : item.jsonSchema.type[0] : [item.jsonSchema.type, 'null'])
                        } else {
                            // 非根节点类型发生变化
                            recurNode(item.jsonSchema, node.id, key);
                        }

                    }
                })
                function recurNode(jsonSchema, id, key) {
                    Object.keys(jsonSchema.properties).forEach(item => {
                        if (jsonSchema.properties[item].id == id) {
                            if (key == 'allow_null') {
                                // 允许NULL
                                vm.$set(jsonSchema.properties[item], 'type', Array.isArray(jsonSchema.properties[item].type) ? jsonSchema.properties[item].type.filter(item => item != 'null').length > 1 ? jsonSchema.properties[item].type.filter(item => item != 'null') : jsonSchema.properties[item].type[0] : [jsonSchema.properties[item].type, 'null'])
                            } else {
                                // 必填
                                if (!jsonSchema.required) {
                                    // 原来没有必填，则新增
                                    vm.$set(jsonSchema, 'required', [item])
                                } else {
                                    if (!jsonSchema.required.includes(item)) {
                                        // 原来有必填，但是不包含当前节点，则新增
                                        jsonSchema.required.push(item)
                                    } else {
                                        // 原来有必填，且包含当前节点，则删除
                                        vm.$set(jsonSchema, 'required', jsonSchema.required.filter(ele => ele != item))
                                    }
                                }
                            }
                        } else {
                            if (jsonSchema.properties[item].properties) {
                                recurNode(jsonSchema.properties[item], id, key);
                            }
                        }
                    })
                }
                resolve(totalSchema)
                commit('SET_DATA_MODEL_DATA', totalSchema)
            })
        },
        // 更新当前节点对应数据类型的详细信息
        updateSchemaTypeDetail({ commit }, params) {
            return new Promise((resolve, reject) => {
                let { totalSchema, currentSchema, node, formData } = params;
                totalSchema.forEach(item => {
                    if (item.id == currentSchema.schema.id) {
                        if (node.root) {
                            // 根节点
                            Object.keys(formData).forEach(key => {
                                if (key != 'dataType' && key != 'const' && key != 'enum') {
                                    if (!Object.keys(item.jsonSchema).includes(key)) {
                                        switch (key) {
                                            case 'allow_null':
                                                // 允许为空
                                                vm.$set(item.jsonSchema, 'type', formData[key] ? Array.isArray(item.jsonSchema.type) ? item.jsonSchema.type : [item.jsonSchema.type, 'null'] : item.jsonSchema.type);
                                                break;
                                            case 'required':
                                                // 根节点没有必填
                                                break;
                                            case 'deprecated':
                                                // 废弃
                                                if (formData['deprecated']) {
                                                    vm.$set(item.jsonSchema, 'deprecated', true)
                                                } else {
                                                    // 非必填
                                                    if (item.jsonSchema.hasOwnProperty('deprecated')) {
                                                        delete item.jsonSchema.deprecated;
                                                    }
                                                }
                                                break;
                                            case 'behavior':
                                                // 行为
                                                if (formData['behavior'] == 'R') {
                                                    vm.$set(item.jsonSchema, 'readOnly', true);
                                                } else if (formData['behavior'] == 'W') {
                                                    vm.$set(item.jsonSchema, 'writeOnly', true);
                                                } else {
                                                    if (item.jsonSchema.hasOwnProperty('readOnly')) {
                                                        delete item.jsonSchema.readOnly;
                                                    }
                                                    if (item.jsonSchema.hasOwnProperty('writeOnly')) {
                                                        delete item.jsonSchema.writeOnly;
                                                    }
                                                }
                                                break;
                                            case 'isConst':
                                                // 常量
                                                if (formData['isConst'] == true) {
                                                    // 当前是常量，把常量值存起来
                                                    vm.$set(item.jsonSchema, 'const', formData['const']);
                                                } else {
                                                    // 当前不是常量，假设之前包含常量值，则删除常量值
                                                    if (item.jsonSchema.hasOwnProperty('const')) {
                                                        delete item.jsonSchema.const;
                                                    }
                                                }
                                                break;
                                            case 'isEnum':
                                                // 枚举
                                                if (formData['isEnum'] == true) {
                                                    // 当前是枚举，把枚举值存起来
                                                    const enumArr = formData['enum'].filter(item => item.value != null).map(item => item.value);
                                                    vm.$set(item.jsonSchema, 'enum', enumArr);
                                                    const enumList = formData['enum'].filter(item => item.value != null);
                                                    vm.$set(item.jsonSchema, 'x-apifox-enum', enumList)
                                                } else {
                                                    // 当前不是枚举，假设之前包含枚举值，则删除枚举值
                                                    if (item.jsonSchema.hasOwnProperty('enum')) {
                                                        delete item.jsonSchema.enum;
                                                    }
                                                    if (item.jsonSchema.hasOwnProperty('x-apifox-enum')) {
                                                        delete item.jsonSchema['x-apifox-enum'];
                                                    }
                                                }
                                                break;
                                            case 'equal_to_minimum':
                                                // 不可等于最小值
                                                if (formData['equal_to_minimum'] == true) {
                                                    vm.$set(item.jsonSchema, 'exclusiveMinimum', formData['minimum']);
                                                } else {
                                                    if (item.jsonSchema.hasOwnProperty('exclusiveMinimum')) {
                                                        delete item.jsonSchema.exclusiveMinimum;
                                                    }
                                                }
                                                break;
                                            case 'equal_to_maximum':
                                                // 不可等于最小值
                                                if (formData['equal_to_maximum'] == true) {
                                                    vm.$set(item.jsonSchema, 'exclusiveMaximum', formData['minimum']);
                                                } else {
                                                    if (item.jsonSchema.hasOwnProperty('exclusiveMaximum')) {
                                                        delete item.jsonSchema.exclusiveMaximum;
                                                    }
                                                }
                                                break;
                                            default:
                                                if (formData[key] != null) {
                                                    vm.$set(item.jsonSchema, key, formData[key]);
                                                } else {
                                                    if (item.jsonSchema.hasOwnProperty(key)) {
                                                        delete item.jsonSchema[key];
                                                    }
                                                }
                                                break;
                                        }
                                    } else {
                                        if (formData[key] != null) {
                                            vm.$set(item.jsonSchema, key, formData[key]);
                                        } else {
                                            if (item.jsonSchema.hasOwnProperty(key)) {
                                                delete item.jsonSchema[key];
                                            }
                                        }
                                    }
                                }
                            })
                        } else {
                            // 非根节点类型发生变化
                            recurNode(item.jsonSchema, node.id);
                        }

                    }
                })
                function recurNode(jsonSchema, id) {
                    Object.keys(jsonSchema.properties).forEach(item => {
                        if (jsonSchema.properties[item].id == id) {
                            Object.keys(formData).forEach(key => {
                                if (key != 'dataType' && key != 'const' && key != 'enum') {
                                    if (!Object.keys(jsonSchema.properties[item]).includes(key)) {
                                        switch (key) {
                                            case 'allow_null':
                                                // 允许为空
                                                vm.$set(jsonSchema.properties[item], 'type', formData['allow_null'] ? Array.isArray(jsonSchema.properties[item].type) ? jsonSchema.properties[item].type : [jsonSchema.properties[item].type, 'null'] : jsonSchema.properties[item].type);
                                                break;
                                            case 'required':
                                                if (formData['required'] && !jsonSchema['required'].includes(item)) {
                                                    // 必填
                                                    jsonSchema['required'].push(item)
                                                } else {
                                                    // 非必填
                                                    jsonSchema['required'].splice(jsonSchema['required'].indexOf(item), 1)
                                                }
                                                break;
                                            case 'deprecated':
                                                // 废弃
                                                if (formData['deprecated']) {
                                                    vm.$set(jsonSchema.properties[item], 'deprecated', true)
                                                } else {
                                                    // 非必填
                                                    if (jsonSchema.properties[item].hasOwnProperty('deprecated')) {
                                                        delete jsonSchema.properties[item].deprecated;
                                                    }
                                                }
                                                break;
                                            case 'behavior':
                                                // 行为
                                                if (formData['behavior'] == 'R') {
                                                    vm.$set(jsonSchema.properties[item], 'readOnly', true);
                                                } else if (formData['behavior'] == 'W') {
                                                    vm.$set(jsonSchema.properties[item], 'writeOnly', true);
                                                }
                                                break;
                                            case 'isConst':
                                                // 常量
                                                if (formData['isConst'] == true) {
                                                    // 当前是常量，把常量值存起来
                                                    vm.$set(jsonSchema.properties[item], 'const', formData['const']);
                                                } else {
                                                    // 当前不是常量，假设之前包含常量值，则删除常量值
                                                    if (jsonSchema.properties[item].hasOwnProperty('const')) {
                                                        delete jsonSchema.properties[item].const;
                                                    }
                                                }
                                                break;
                                            case 'isEnum':
                                                // 枚举
                                                if (formData['isEnum'] == true) {
                                                    // 当前是枚举，把枚举值存起来
                                                    const enumArr = formData['enum'].filter(item => item.value != null).map(item => item.value);
                                                    vm.$set(jsonSchema.properties[item], 'enum', enumArr);
                                                    const enumList = formData['enum'].filter(item => item.value != null);
                                                    vm.$set(jsonSchema.properties[item], 'x-apifox-enum', enumList);
                                                } else {
                                                    // 当前不是枚举，假设之前包含枚举值，则删除枚举值
                                                    if (jsonSchema.properties[item].hasOwnProperty('enum')) {
                                                        delete jsonSchema.properties[item].enum;
                                                    }
                                                    if (jsonSchema.properties[item].hasOwnProperty('x-apifox-enum')) {
                                                        delete jsonSchema.properties[item]['x-apifox-enum'];
                                                    }

                                                }
                                                break;
                                            case 'equal_to_minimum':
                                                // 不可等于最小值
                                                if (formData['equal_to_minimum'] == true) {
                                                    vm.$set(jsonSchema.properties[item], 'exclusiveMinimum', formData['minimum']);
                                                } else {
                                                    if (jsonSchema.properties[item].hasOwnProperty('exclusiveMinimum')) {
                                                        delete jsonSchema.properties[item].exclusiveMinimum;
                                                    }
                                                }
                                                break;
                                            case 'equal_to_maximum':
                                                // 不可等于最小值
                                                if (formData['equal_to_maximum'] == true) {
                                                    vm.$set(jsonSchema.properties[item], 'exclusiveMaximum', formData['minimum']);
                                                } else {
                                                    if (jsonSchema.properties[item].hasOwnProperty('exclusiveMaximum')) {
                                                        delete jsonSchema.properties[item].exclusiveMaximum;
                                                    }
                                                }
                                                break;
                                            default:
                                                if (formData[key] != null) {
                                                    vm.$set(jsonSchema.properties[item], key, formData[key]);
                                                } else {
                                                    if (jsonSchema.properties[item].hasOwnProperty(key)) {
                                                        delete jsonSchema.properties[item][key];
                                                    }
                                                }
                                                break;
                                        }
                                    } else {
                                        if (formData[key] != null) {
                                            vm.$set(jsonSchema.properties[item], key, formData[key]);
                                        } else {
                                            if (jsonSchema.properties[item].hasOwnProperty(key)) {
                                                delete jsonSchema.properties[item][key];
                                            }
                                        }
                                    }
                                }
                            })
                        } else {
                            if (jsonSchema.properties[item].properties) {
                                recurNode(jsonSchema.properties[item], id);
                            }
                        }
                    })
                }
                resolve(totalSchema)
                commit('SET_DATA_MODEL_DATA', totalSchema)
            })
        },
        // 删除节点
        deleteSchemaStructure({ commit }, params) {
            return new Promise((resolve, reject) => {
                let { totalSchema, currentSchema, data } = params;
                totalSchema.forEach(item => {
                    if (item.id == currentSchema.schema.id) {
                        recurNode(item.jsonSchema, data);
                    }
                })
                function recurNode(jsonSchema, data) {
                    Object.keys(jsonSchema.properties).forEach(item => {
                        if (jsonSchema.properties[item].id == data.id) {
                            // 删除当前节点
                            delete jsonSchema.properties[item];
                            // 删除排序数据中的节点
                            if (jsonSchema['x-apifox-orders'] && jsonSchema['x-apifox-orders'].includes(item)) {
                                jsonSchema['x-apifox-orders'].splice(jsonSchema['x-apifox-orders'].indexOf(item), 1)
                            }
                            // 删除必填数据中的节点
                            if (jsonSchema['required'] && jsonSchema['required'].includes(item)) {
                                jsonSchema['required'].splice(jsonSchema['required'].indexOf(item), 1)
                            }
                            // 删除ref中的引用
                            if (jsonSchema['x-apifox-refs']) {
                                Object.keys(jsonSchema['x-apifox-refs']).forEach(key => {
                                    if (!jsonSchema['x-apifox-orders'].includes(key)) {
                                        delete jsonSchema['x-apifox-refs'][key]
                                    }
                                })
                            }
                        } else {
                            if (jsonSchema.properties[item]['properties']) {
                                recurNode(jsonSchema.properties[item], data);
                            }
                        }
                    })
                }
                resolve(totalSchema)
                commit('SET_DATA_MODEL_DATA', totalSchema)
            })
        }
    }
}
export default model_scenario